define(['jquery', 'bootstrap', 'frontend', 'form', 'template'], function ($, undefined, Frontend, Form, Template) {
    var validatoroptions = {
        invalid: function (form, errors) {
            $.each(errors, function (i, j) {
                Layer.msg(j);
            });
        }
    };
    var Controller = {
        login: function () {
            //本地验证未通过时提示
            $("#login-form").data("validator-options", validatoroptions);

            $(document).on("change", "input[name=type]", function () {
                var type = $(this).val();
                $("div.form-group[data-type]").addClass("hide");
                $("div.form-group[data-type='" + type + "']").removeClass("hide");
                $('#resetpwd-form').validator("setField", {
                    captcha: "required;length(4);integer[+];remote(" + $(this).data("check-url") + ", event=resetpwd, " + type + ":#" + type + ")",
                });
                $(".btn-captcha").data("url", $(this).data("send-url")).data("type", type);
            });

            //为表单绑定事件
            Form.api.bindevent($("#login-form"), function (data, ret) {
                setTimeout(function () {
                    location.href = ret.url ? ret.url : "/";
                }, 1000);
            });

            Form.api.bindevent($("#resetpwd-form"), function (data) {
                Layer.closeAll();
            });

            $(document).on("click", ".btn-forgot", function () {
                var id = "resetpwdtpl";
                var content = Template(id, {});
                Layer.open({
                    type: 1,
                    title: __('Reset password'),
                    area: ["450px", "355px"],
                    content: content,
                    success: function (layero) {
                        Form.api.bindevent($("#resetpwd-form", layero), function (data) {
                            Layer.closeAll();
                        });
                    }
                });
            });
        },
        register: function () {
            //本地验证未通过时提示
            $("#register").data("validator-options", validatoroptions);

            //为表单绑定事件
            Form.api.bindevent($("#register"), function (data, ret) {
                setTimeout(function () {
                    location.href = ret.url ? ret.url : "/";
                }, 1000);
            });

            $('#changeStyle').on('click', function() {
                var account=document.getElementById('account').value;
                if (!account) {
                     layer.msg('请输入手机号码/获邮箱');
                      location.reload();
                     return false;
                }
                var re = /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/;
                if (re.test(account)) {
                   $.post('/index/ems/send',{email:account,event:'emsCode'},function(rsp){
                        layer.closeAll();
                        layer.msg(rsp.msg, {
                          offset: 'auto',
                          anim: 6,
                          end:function(){
                           
                          }
                        });
                       
                    })
                  } else {
                  
                    var phoneReg = /(^1[3|4|5|7|8]\d{9}$)|(^09\d{8}$)/;
                    if(!phoneReg.test(account)){
                        layer.msg('手机有误')
                         location.reload();
                     }else{
                        $.post('/index/sms/send',{mobile:account,event:'smsCode'},function(rsp){
                        layer.closeAll();
                        layer.msg(rsp.msg, {
                          offset: 'auto',
                          anim: 6,
                          end:function(){
                           
                          }
                        });
                        
                    })
                    }
                   
                  } 
               
            });

        },
        resetpwd:function(){

            $("#resetpwd").data("validator-options", validatoroptions);

            //为表单绑定事件
            Form.api.bindevent($("#resetpwd"), function (data, ret) {
                setTimeout(function () {
                    location.href = ret.url ? ret.url : "/";
                }, 1000);
            });
            $('#changecode').on('click', function() {
                var account=document.getElementById('account').value;
                if (!account) {
                     layer.msg('请输入手机号码/获邮箱');
                      location.reload();
                     return false;
                }
                var re = /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/;
                if (re.test(account)) {
                   $.post('/index/ems/send',{email:account,event:'resetpwd'},function(rsp){
                        layer.closeAll();
                        layer.msg(rsp.msg, {
                          offset: 'auto',
                          anim: 6,
                          end:function(){
                           
                          }
                        });
                       
                    })
                  } else {
                  
                    var phoneReg = /(^1[3|4|5|7|8]\d{9}$)|(^09\d{8}$)/;
                    if(!phoneReg.test(account)){
                        layer.msg('手机有误')
                         location.reload();
                     }else{
                        $.post('/index/sms/send',{mobile:account,event:'resetpwd'},function(rsp){
                        layer.closeAll();
                        layer.msg(rsp.msg, {
                          offset: 'auto',
                          anim: 6,
                          end:function(){
                           
                          }
                        });
                        
                    })
                    }
                   
                  } 
               
            });
        },
        changepwd: function () {
            //本地验证未通过时提示
            $("#changepwd-form").data("validator-options", validatoroptions);

            //为表单绑定事件
            Form.api.bindevent($("#changepwd-form"), function (data, ret) {
                setTimeout(function () {
                    location.href = ret.url ? ret.url : "/";
                }, 1000);
            });
        },
        profile: function () {
            // 给上传按钮添加上传成功事件
            $("#plupload-avatar").data("upload-success", function (data) {
                var url = Fast.api.cdnurl(data.url);
                $(".profile-user-img").prop("src", url);
                Toastr.success(__('Upload successful'));
            });
            Form.api.bindevent($("#profile-form"));
            $(document).on("click", ".btn-change", function () {
                var that = this;
                var id = $(this).data("type") + "tpl";
                var content = Template(id, {});
                Layer.open({
                    type: 1,
                    title: "修改",
                    area: ["400px", "250px"],
                    content: content,
                    success: function (layero) {
                        var form = $("form", layero);
                        Form.api.bindevent(form, function (data) {
                            location.reload();
                            Layer.closeAll();
                        });
                    }
                });
            });
        }
    };
    return Controller;
});